package com.aaa.vip.service.impl;

import com.aaa.common.entity.UserBalance;
import com.aaa.common.util.TokenUtil;
import com.aaa.vip.dao.UserBalanceDao;
import com.aaa.vip.service.UserBalanceService;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.ResultStatus;
import com.aaa.common.vo.Page;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * (UserBalance)表服务实现类
 *
 * @author makejava
 * @since 2025-09-25 19:08:48
 */
@Service("userBalanceService")
public class UserBalanceServiceImpl implements UserBalanceService {
    @Resource
    private UserBalanceDao userBalanceDao;
    @Resource
    private HttpServletRequest request;

    /**
     * 通过TokenID查询单条数据
     *
     * @param
     * @return 实例对象
     */
    @Override
    public UserBalance selectMoneyByUserId() {
        String userId = TokenUtil.getUserId(request);
        return this.userBalanceDao.selectMoneyByUserId(Integer.valueOf(userId));
    }
    @Override
    public UserBalance selectMoneyByUserIdUseToken(Integer userId) {
        return this.userBalanceDao.selectMoneyByUserId(userId);
    }
    @Override
    public UserBalance selectMoneyByUserIdAndStoreId(Integer userId, Integer storeId) {
        return this.userBalanceDao.selectMoneyByUserIdAndStoreId(userId,storeId);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param balanceId 主键
     * @return 实例对象
     */
    @Override
    public UserBalance queryById(Integer balanceId) {
        return this.userBalanceDao.queryById(balanceId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<UserBalance> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        UserBalance userBalance = page.getData();
        return new PageInfo(userBalanceDao.queryAllByLimit(userBalance));
    }

    /**
     * 新增数据
     *
     * @param userBalance 实例对象
     * @return 实例对象
     */
    @Override
    public UserBalance insert(UserBalance userBalance) {
        this.userBalanceDao.insert(userBalance);
        return userBalance;
    }

    /**
     * 修改数据
     *
     * @param userBalance 实例对象
     * @return 实例对象
     */
    @Override
    public UserBalance update(UserBalance userBalance) {
        this.userBalanceDao.update(userBalance);
        return this.queryById(userBalance.getBalanceId());
    }

    /**
     * 通过主键删除数据
     *
     * @param balanceId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer balanceId) {
        return this.userBalanceDao.deleteById(balanceId) > 0;
    }
}
